package com.yanqu.road.server.manger.drugstore;

import com.yanqu.road.entity.drugstore.config.DrugStoreBadgeInfo;
import com.yanqu.road.entity.drugstore.config.DrugStoreDrugUpgradeInfo;
import com.yanqu.road.entity.drugstore.config.DrugStorePrescriptionInfo;
import com.yanqu.road.logic.bussiness.player.DrugStoreBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class DrugStoreConfigMgr extends TempMgr {

    static Map<Integer, DrugStorePrescriptionInfo> prescriptionInfoMap;

    static private List<DrugStoreBadgeInfo> storeBadgeInfoList;

    static private List<DrugStoreDrugUpgradeInfo> upgradeInfoList;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        prescriptionInfoMap = DrugStoreBussiness.getDrugStorePrescriptionInfoMap();
        storeBadgeInfoList = DrugStoreBussiness.getDrugStoreBadgeInfoList();
        upgradeInfoList = DrugStoreBussiness.getDrugStoreDrugUpgradeInfoList();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static DrugStorePrescriptionInfo getDrugStorePrescriptionInfo(int id){
        return prescriptionInfoMap.get(id);
    }

    /**
     * 随机所有处方
     * @param type  1：普通；2：疑难杂症
     * @return
     */
    public static DrugStorePrescriptionInfo randomPrescription(int type){
        List<DrugStorePrescriptionInfo> list = new ArrayList<>();
        for (Map.Entry<Integer, DrugStorePrescriptionInfo> infoEntry : prescriptionInfoMap.entrySet()) {
            if(infoEntry.getValue().getType() == type){
                list.add(infoEntry.getValue());
            }
        }
        if(list.size() == 0){
            return null;
        }
        int idx = new Random().nextInt(list.size());
        return list.get(idx);
    }

    /**
     * 随机已解锁的处方（按权重）
     * @param storeLv  药铺等级
     * @return
     *
     */
    public static DrugStorePrescriptionInfo randomUnLockChuFang(int storeLv){
        DrugStoreBadgeInfo badgeInfo = getDrugStoreBadgeInfo(storeLv);
        if(badgeInfo == null){
            return null;
        }
        List<String> typeWeightList = StringUtils.stringToStringList(badgeInfo.getExParam(), "#");
        if(typeWeightList.size() > 0){
            List<String> weightItemList = StringUtils.stringToStringList(typeWeightList.get(0), "\\|");
            List<Integer> idList = new ArrayList<>();
            List<Integer> weightList = new ArrayList<>();
            for (String str : weightItemList) {
                List<Integer> idWeightList = StringUtils.stringToIntegerList(str, ";");
                if(idWeightList.size() == 2){
                    idList.add(idWeightList.get(0));
                    weightList.add(idWeightList.get(1));
                }
            }
            int idx = RandomHelper.getRandomIndexByWeight(weightList);
            int id = idList.get(idx);
            return getPrescriptionInfoMap().get(id);
        }
        return null;
    }

    /**
     * 获取解锁的处方/药方
     */
    public static List<DrugStorePrescriptionInfo> getUnLock(int storeLv){
        List<DrugStorePrescriptionInfo> result = new ArrayList<>();
        for (Map.Entry<Integer, DrugStorePrescriptionInfo> infoEntry : prescriptionInfoMap.entrySet()) {
            if(infoEntry.getValue().getUnLock() == storeLv && infoEntry.getValue().getType() == 1){
                result.add(infoEntry.getValue());
            }
        }
        return result;
    }

    /**
     * 获取处方升级配置
     */
    public static DrugStoreDrugUpgradeInfo getDrugStoreDrugUpgradeInfo(int chuFang, int level){
        for (int i = 0; i < upgradeInfoList.size(); i++) {
            DrugStoreDrugUpgradeInfo upgradeInfo = upgradeInfoList.get(i);
            if(upgradeInfo.getPrescriptionId() == chuFang && upgradeInfo.getLevel() == level){
                return upgradeInfo;
            }
        }
        return null;
    }

    /**
     * 获取药铺升级条件（下一级）
     */
    public static DrugStoreBadgeInfo getNextLevelDrugStoreBadgeInfo(int level){
        if(level == storeBadgeInfoList.size()){
            return null;
        }
        for (int i = 0; i < storeBadgeInfoList.size(); i++) {
            DrugStoreBadgeInfo badgeInfo = storeBadgeInfoList.get(i);
            if(badgeInfo.getLevel() == level){
                return storeBadgeInfoList.get(i + 1);
            }
        }
        return null;
    }

    public static DrugStoreBadgeInfo getDrugStoreBadgeInfo(int level){
        if(level > storeBadgeInfoList.size()){
            return null;
        }
        for (int i = 0; i < storeBadgeInfoList.size(); i++) {
            DrugStoreBadgeInfo badgeInfo = storeBadgeInfoList.get(i);
            if(badgeInfo.getLevel() == level){
                return badgeInfo;
            }
        }
        return null;
    }

    /**
     * 根据药铺等级获取解锁项目
     * @param storeLv
     * @param type 项目类型：1：农田；2：诊桌；3：药师（补药师、账房先生）
     * @return
     */
    public static List<Integer> getBadgeInfoSpecialEffect(int storeLv, int type){
        List<Integer> result = new ArrayList<>();
        DrugStoreBadgeInfo badgeInfo = getDrugStoreBadgeInfo(storeLv);
        if(badgeInfo != null){
            List<String> itemList = StringUtils.stringToStringList(badgeInfo.getSpecialEffect(), ";");
            for (String itemStr : itemList) {
                List<Integer> list = StringUtils.stringToIntegerList(itemStr, "-");
                if(list.get(0) == type){
                    result.add(list.get(1));
                }
            }
        }
        return result;
    }

    public static Map<Integer, DrugStorePrescriptionInfo> getPrescriptionInfoMap() {
        return prescriptionInfoMap;
    }

    public static List<DrugStoreBadgeInfo> getStoreBadgeInfoList() {
        return storeBadgeInfoList;
    }

    public static List<DrugStoreDrugUpgradeInfo> getUpgradeInfoList() {
        return upgradeInfoList;
    }
}
